#pragma once

#include <iostream>
#include <string.h>
#include "assert.h"
namespace dys{
    using namespace std;
    class string{
        typedef char* iterator;
        typedef const char* const_iterator;
    public:
        string(const char* c_str = "");
        // string();
        ~string();
        string(const string& str);

    public:
        const char *c_str() const{
            return _str;
        }
        size_t size() const{
            return _size;
        }
        void reserve(size_t n);
        void append(const char* str);
        void push_back(char ch);
        void insert(size_t pos, char ch);
        void insert(size_t pos, const char* str);
        void erase(size_t pos, size_t len = npos);
        size_t find(char ch, size_t pos = 0);
        size_t find(const char* str, size_t pos = 0);
        string& operator=(const string& str);
    public:
        string& operator+=(char ch){
            push_back(ch);
            return *this;
        }
        string& operator+=(const char* str){
            append(str);
            return *this;
        }
        char& operator[] (int pos) {        // 调用 const 版本的 operator[]避免代码重复
            return const_cast<char&>(
                static_cast<const string&>(*this)[pos]
            );
        }
        const char& operator[] (int pos) const{
            assert(pos < _size);
            return _str[pos];
        }

        iterator begin(){
            return _str;
        }
        iterator end(){
            return _str + _size;
        }
        const_iterator begin() const{
            return _str;
        }
        const_iterator end() const{
            return _str + _size;
        }
        
    private:
        void insert_aux(const char* str,int pos, int len);

    private:
        char* _str;
        size_t _size;
        size_t _capacity;
        const static size_t npos = -1; // 整数类型的特殊规则
    };
};